package hot;

import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;

public class Num406 {
    public int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people,new Comparator<int[]>() {
            @Override
            public int compare(int[] o1,int[] o2) {
                if(o1[0] != o2[0]) {
                    return o2[0] - o1[0];
                }else {
                    return o1[1] - o2[1];
                }
            }
        });
        LinkedList<int[]> res = new LinkedList<>();
        for(int i = 0; i < people.length; i++) {
            if(i <= people[i][1]) {
                res.add(people[i]);
            }else {
                res.add(people[i][1],people[i]);
            }
        }
        int[][] arr = new int[people.length][2];
        int index = 0;
        for(int[] nums : res) {
            arr[index++] = nums;
        }
        return arr;
    }
}
